﻿using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ProcecureSubPriceBusiness : BaseBusiness<PB_ProcecureSubPrice>, IPB_ProcecureSubPriceBusiness, ITransientDependency
    {
        public PB_ProcecureSubPriceBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        /// <summary>
        /// 查询工序单价
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<ProcecureSubPriceDTO>> GetDataListAsync(PageInput<SubPriceConditionDTO> input)
        {
            var search = input.Search;
            var where = LinqHelper.True<ProcecureSubPriceDTO>();
            Expression<Func<PB_ProcecureSubPrice, PB_ProcecureSub, ProcecureSubPriceDTO>> select = (a, b) => new ProcecureSubPriceDTO
            {
                SubName = b.Name
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in Db.GetIQueryable<PB_ProcecureSubPrice>().AsExpandable()
                    join b in Db.GetIQueryable<PB_ProcecureSub>() on a.SubId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            q = q.Where(x => x.SubId == search.SubId);
            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_ProcecureSubPrice> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_ProcecureSubPrice data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_ProcecureSubPrice data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task AddDataExlAsync(List<PB_MaterialPrice> list)
        {
            await Db.InsertAsync<PB_MaterialPrice>(list);
        }
        /// <summary>
        /// 添加时间段的工序价格
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task InsertSubPriceAsync(SubPriceDTO data)
        {
            DateTime StartDate = Convert.ToDateTime(data.StartDate);
            DateTime EndDate = Convert.ToDateTime(data.EndDate);
            TimeSpan ts = EndDate.Subtract(StartDate);
            int days = ts.Days + 1;
            List<PB_ProcecureSubPrice> listdata = new List<PB_ProcecureSubPrice>();
            for (int i = 0; i < days; i++)
            {
                PB_ProcecureSubPrice pdata = new PB_ProcecureSubPrice();
                pdata.Id = IdHelper.GetId();
                pdata.SubId = data.SubId;
                pdata.PriceDate = StartDate.AddDays(i);
                pdata.Price = data.Price;
                pdata.Type = data.Type;
                pdata.Upperlimit = data.Upperlimit;
                pdata.Lowerlimit = data.Lowerlimit;
                listdata.Add(pdata);
            }
            await Db.InsertAsync<PB_ProcecureSubPrice>(listdata);
        }
        public IQueryable<T> GetQueryable<T>() where T : class, new()
        {
            return Db.GetIQueryable<T>();
        }
        #endregion

        #region 私有成员

        #endregion
    }
}